package com.zb.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Assert;
import com.baomidou.mybatisplus.core.toolkit.BeanUtils;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zb.entity.ZjCustomerAuthor;
import com.zb.entity.vo.CustomerAuthorDetailVo;
import com.zb.entity.vo.CustomerAuthorVo;
import com.zb.mapper.ZjCustomerAuthorMapper;
import com.zb.service.ZjCustomerAuthorService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Objects;

/**
 * <p>
 * 商家权限菜单表 服务实现类
 * </p>
 *
 * @author WEN
 * @since 2024-02-20
 */
@Service
public class ZjCustomerAuthorServiceImpl extends ServiceImpl<ZjCustomerAuthorMapper, ZjCustomerAuthor> implements ZjCustomerAuthorService {

    // 根据商家企业ID删除菜单权限
    @Override
    public void deleteByCustomerIdAndAuthorId(Long custAccId, List<Long> delAuthorIds) {
        if (CollectionUtil.isEmpty(delAuthorIds)) {
            return;
        }
        QueryWrapper<ZjCustomerAuthor> wrapper = new QueryWrapper<>();
        wrapper.eq("cust_acc_id", custAccId).in("author_id", delAuthorIds);
        this.remove(wrapper);
    }

    // 根据企业ID获取企业的模块菜单权限
    @Override
    public List<CustomerAuthorDetailVo> getCustomerAuthor(Long id) {
        QueryWrapper<ZjCustomerAuthor> wrapper = new QueryWrapper<>();
        wrapper.select("author_id", "parent_author_id", "name", "sort", "is_disabled AS disabled")
                .eq("cust_acc_id", id);
        List<ZjCustomerAuthor> customerAuthorList = this.list(wrapper);
        // 获取父级数据
        List<CustomerAuthorDetailVo> voList = new ArrayList<>();
                // 获取子级数据
        List<ZjCustomerAuthor> childList = new ArrayList<>();
        customerAuthorList.stream().forEach(author -> {
            if (author.getParentAuthorId() == 0) {
                CustomerAuthorDetailVo vo = new CustomerAuthorDetailVo();
                BeanUtil.copyProperties(author, vo);
                vo.setStatus(author.getDisabled());
                vo.setAuthorDetailVos(new ArrayList<>());
                voList.add(vo);
            } else {
                childList.add(author);
            }
        });
        for (CustomerAuthorDetailVo vo : voList) {
            vo.setAuthorDetailVos(getChilCustomerAuthor(vo.getAuthorId(), childList));
        }
        return voList;
    }

    private List<CustomerAuthorDetailVo> getChilCustomerAuthor(Long authorId, List<ZjCustomerAuthor> customerAuthorList) {
        return customerAuthorList.stream().filter(f -> Objects.equals(f.getParentAuthorId(), authorId)).map(author -> {
            CustomerAuthorDetailVo vo = new CustomerAuthorDetailVo();
            vo.setAuthorId(author.getAuthorId());
            vo.setName(author.getName());
            vo.setSort(author.getSort());
            vo.setStatus(author.getDisabled());
            vo.setAuthorDetailVos(getChilCustomerAuthor(author.getAuthorId(), customerAuthorList));
            return vo;
        }).toList();
    }

}
